/*
MIT License

Copyright (c) 2007 Ralf Kornmann

All right reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/

#pragma once

enum AddressMode
{
  AM_A,
  AM_C,
  AM_D,
  AM_E,
  AM_F,
  AM_G,
  AM_I,
  AM_J,
  AM_M,
  AM_O,
  AM_P,
  AM_Q,
  AM_R,
  AM_S,
  AM_T,
  AM_V,
  AM_W,
  AM_X,
  AM_Y,
  AM_Unused
};

enum OperatorType
{
  OT_a,
  OT_b,
  OT_c,
  OT_d,
  OT_dq,
  OT_p,
  OT_pi,
  OT_ps,
  OT_q,
  OT_s,
  OT_ss,
  OT_si,
  OT_v,
  OT_w,
  OT_Unused
};

enum RMType
{
  RMT_No,
  RMT_Register,
  RMT_BYTE
};

enum SIBType
{
  SIBT_No,
  SIBT_BYTE,
  SIBT_Check
};

enum DisplacementType
{
  DT_No,
  DT_Check
};

enum ImmediateType
{
  IT_No,
  IT_Fix1,
  IT_Fix2,
  IT_Fix3,
  IT_Fix4,
  IT_OpSize,
  IT_Addr,
  IT_AddrSize
};

struct OpCode
{
  BYTE m_Code;
  LPCSTR m_Name;

  BOOL m_Stop;
  BOOL m_RelJump;

  RMType m_RMType;
  SIBType m_SIBType;
  DisplacementType m_DispType;
  ImmediateType m_IType;
  
  BYTE m_PrefixGroup;
  OpCode* m_pEscape;
  OpCode* m_pRMEscape;
};

class CASMInstruction
{
  public:
    CASMInstruction ();

    bool Parse (BYTE* pInst);
    bool Transfer (BYTE* pSource, BYTE* pDest);

    DWORD GetSize ();
    DWORD GetImmediateSize ();
    DWORD GetJumpPatchSize ();

    bool IsStop ();
    bool IsRelJump ();

  protected:
    int m_DefaultAddrMode;
    int m_AlternatAddrMode;
    int m_DefaultOpcodeSize;
    int m_AlternatOpCodeSize;

    OpCode* m_pPrefix[4];

    OpCode* m_OpCode1;
    OpCode* m_OpCode2;
    OpCode* m_RMOpCode;

    OpCode* m_LastOpCode;

    int m_Mod;
    int m_Reg;
    int m_RM;

    int m_Scale;
    int m_Index;
    int m_Base;

    long m_DisplacementSize;
    DWORD m_Displacement;

    long m_ImmediateSize;
    DWORD m_Immediate;

    long m_Size;

    void Reset ();
    void ReadRMRegister (BYTE rm);
    void ReadRMByte (BYTE rm);

    void ReadSIB (BYTE SIB);
    bool CheckSIB ();
    bool CheckSIB16 ();
    bool CheckSIB32 ();

    int CheckDisp ();
    int CheckDisp16 ();
    int CheckDisp32 ();

    int GetAddressMode ();
    int GetOpcodeSize ();

    bool TransferEB (BYTE* pSource, BYTE* pDest);
};
